package com.meilai.project.service.finance.paymentReceived.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.entity.business.sale.BillSaleGroupRelation;
import com.meilai.project.entity.finance.paymentReceived.AccountReceived;
import com.meilai.project.mapper.business.sale.BillSaleMapper;
import com.meilai.project.mapper.finance.paymentReceived.AccountReceivedMapper;
import com.meilai.project.service.business.sale.BillSaleGroupRelationService;
import com.meilai.project.service.finance.paymentReceived.AccountReceivedService;
import com.meilai.project.util.ThreadLocalUserUtil;
import com.meilai.project.vo.business.sale.BillSaleVO;
import com.meilai.project.vo.finance.paymentReceived.AccountReceivedVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 到款表 服务实现类
 * </p>
 *
 * @author xhy
 * @since 2022-12-02
 */
@Service
public class AccountReceivedServiceImpl extends ServiceImpl<AccountReceivedMapper, AccountReceived> implements AccountReceivedService {

    @Autowired
    private BillSaleGroupRelationService billSaleGroupRelationService;

    @Autowired
    private BillSaleMapper billSaleMapper;

    @Override
    public Page<AccountReceivedVO> page(Integer current_page, Integer page_size, LocalDateTime begin_time, LocalDateTime end_time, Long customer_id) {
        Page<AccountReceivedVO> page = new Page<>(current_page, page_size);
        List<AccountReceivedVO> list = baseMapper.page(page, begin_time, end_time, customer_id, null);
        this.handle(list);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<AccountReceivedVO> salePage(Integer current_page, Integer page_size, LocalDateTime begin_time, LocalDateTime end_time, Long customer_id) {
        Long user_id = ThreadLocalUserUtil.get().getId();
        Page<AccountReceivedVO> page = new Page<>(current_page, page_size);
        List<AccountReceivedVO> list = baseMapper.page(page, begin_time, end_time, customer_id, user_id);
        this.handle(list);
        page.setRecords(list);
        return page;
    }

    @Override
    public Integer countUnHandled() {
        Long user_id = ThreadLocalUserUtil.get().getId();
        return baseMapper.countUnHandled(user_id);
    }

    @Override
    public List<AccountReceivedVO> queryListByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            ids = new ArrayList<>();
            ids.add(-1L);
        }
        List<AccountReceivedVO> list = baseMapper.queryListByIds(ids);
        this.handle(list);
        return list;
    }

    public void handle(List<AccountReceivedVO> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            List<Long> group_ids = list.stream().filter(item -> item.getGroup_id() != null).map(AccountReceivedVO::getGroup_id).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(group_ids)) {
                List<BillSaleGroupRelation> billSaleGroupRelationList = billSaleGroupRelationService.list(Wrappers.<BillSaleGroupRelation>lambdaQuery().in(BillSaleGroupRelation::getGroup_id, group_ids));
                List<Long> bill_ids = billSaleGroupRelationList.stream().map(BillSaleGroupRelation::getBill_sale_id).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(bill_ids)) {
                    Map<Long, List<Long>> group_sale_map = billSaleGroupRelationList.stream().collect(Collectors.groupingBy(BillSaleGroupRelation::getGroup_id, Collectors.mapping(BillSaleGroupRelation::getBill_sale_id, Collectors.toList())));
                    List<BillSaleVO> billSaleVOS = billSaleMapper.selectListOfIds(bill_ids);
                    list.forEach(item -> {
                        List<Long> ids = group_sale_map.get(item.getGroup_id());
                        if (CollectionUtils.isNotEmpty(ids)) {
                            item.setBillSaleVOS(billSaleVOS.stream().filter(sale -> ids.contains(sale.getId())).collect(Collectors.toList()));
                        } else {
                            item.setBillSaleVOS(new ArrayList<>());
                        }
                    });
                } else {
                    list.forEach(item -> {
                        item.setBillSaleVOS(new ArrayList<>());
                    });
                }
            } else {
                list.forEach(item -> {
                    item.setBillSaleVOS(new ArrayList<>());
                });
            }
        }
    }
}
